package cn.edu.cxcyzx.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.edu.cxcyzx.algorithm.CalMd5;
import cn.edu.cxcyzx.entity.User;
import cn.edu.cxcyzx.service.UserService;
import cn.edu.cxcyzx.supervisor.DaoFit;
import cn.edu.cxcyzx.supervisor.ResultDo;
import cn.edu.cxcyzx.util.PropertyPlaceholder;

@Service("UserService")
public class UserServiceImpl implements UserService {
	
	@Resource
	public DaoFit daoFit;
	
	@Override
	public void insertUser(User u) {
		// TODO Auto-generated method stub
		String default_password=PropertyPlaceholder.getProperty("DEFAULT_PASSWORD");
		u.setSecret(CalMd5.MD5(default_password, u.getAccount()));
		daoFit.getUserDao().insertUser(u);
	}

	@Override
	public User getUser(String account) {
		// TODO Auto-generated method stub
		return daoFit.getUserDao().getUser(account);
	}
	@Override
	public void updateSecret(String account, String new_password) {
		// TODO Auto-generated method stub
		User u=new User(account, CalMd5.MD5(new_password, account));
		daoFit.getUserDao().updateSecret(u);
	}

	@Override
	public ResultDo<Object> login(String account, String pwd) {
		// TODO Auto-generated method stub
		ResultDo<Object> resultDo=new ResultDo<>();
		String input_md5=CalMd5.MD5(pwd, account);
		System.out.println("input_md5:"+input_md5);
		User u=daoFit.getUserDao().getUser(account);
		if(u==null){
			resultDo.setSuccess(false);
			resultDo.setMessage("账号不存在！");
		}else if(!input_md5.equals(u.getSecret())){
			System.out.println("secret:"+u.getSecret());
			resultDo.setSuccess(false);
			resultDo.setMessage("密码错误！");
		}else{
			resultDo.setSuccess(true);
			resultDo.setFalg(u.getRole());
			switch (u.getRole()) {
			case 2:
				resultDo.setResult(daoFit.getStudentDao().getStudent(u.getAccount()));
				break;
			case 3:
				resultDo.setResult(daoFit.getTeacherDao().selectByTeaId(u.getAccount()));
				break;
			default:
				resultDo.setResult(daoFit.getAdminDao().getAdminById(u.getAccount()));
				break;
			}
		}
		return resultDo;
	}
	

}
